#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "stu_mis.h"
#include <stdlib.h>

void stu_find()
{
    struct student s;

    int reg = find_menu();

    printf("\t┏━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
    printf("\t┃           欢迎使用学生管理系统BetaV1.3           ┃\n");
    printf("\t┃                               by hht      ┃\n");

    FILE *fp = fopen("student_info.txt", "r");
    switch (reg)
    {
    case 1:
        int num;
        while (1)
        {
            printf("\t┃请输入要查找学生的学号：");
            if (1 == scanf("%d", &num))
            {
                if (num < STU_NUM_MIN || num > STU_NUM_MAX)
                {
                    printf("\t┃输入范围错误，请从新输入！\n");
                    continue;
                }
                break;
            }
            printf("\t┃输入错误，请从新输入！┃\n");
            fflush(stdin); // 刷新输入缓冲区
        }

        fseek(fp, 0, SEEK_SET);
        while (fread(&s, sizeof(s), 1, fp) == 1)
        {
            if (s.num == num)
            {
                printf("\t┃学号：%d->->->姓名：%s->->->性别：%s->->->成绩：%f┃\n", s.num, s.name, s.sex, s.grades);
            }
        }
        fclose(fp);
        break;

    case 2:
        char name[50];
        while (1)
        {

            printf("\t┃请输入要查找学生的姓名：");
            if (1 == scanf("%s", s.name))
            {
                break;
            }
            printf("\t┃输入错误，请从新输入！┃\n");
            fflush(stdin); // 刷新输入缓冲区
        }

        fseek(fp, 0, SEEK_SET);
        while (fread(&s, sizeof(s), 1, fp) == 1)
        {
            if (0 == strcmp(s.name, name))
            {
                printf("\t┃学号：%d->->->姓名：%s->->->性别：%s->->->成绩：%f┃\n", s.num, s.name, s.sex, s.grades);
            }
        }
        fclose(fp);
        break;

    case 3:
        char sex[4];
        while (1)
        {
            printf("\t┃请输入学生性别（男OR女）：");
            if (1 == scanf("%s", s.sex))
            {
                if ((strcmp(s.sex, "男") != 0) && (strcmp(s.sex, "女") != 0))
                {
                    printf("\t┃输入错误，请重新输入！┃\n");
                    continue;
                }
                break;
            }
            printf("\t┃输入错误，请从新输入！┃\n");
            fflush(stdin); // 刷新输入缓冲区
        }

        fseek(fp, 0, SEEK_SET);
        while (fread(&s, sizeof(s), 1, fp) == 1)
        {
            if (0 == strcmp(s.sex, sex))
            {
                printf("\t┃学号：%d->->->姓名：%s->->->性别：%s->->->成绩：%f┃\n", s.num, s.name, s.sex, s.grades);
            }
        }
        fclose(fp);
        break;

    case 4:
        float grades;
        while (1)
        {
            printf("\t┃请输入学生成绩：");
            if (1 == scanf("%f", &s.grades))
            {
                break;
            }
            printf("\t┃输入错误，请从新输入！┃\n");
            fflush(stdin); // 清空输入缓冲区
        }

        fseek(fp, 0, SEEK_SET);
        while (fread(&s, sizeof(s), 1, fp) == 1)
        {
            if (s.grades == grades)
            {
                printf("\t┃学号：%d->->->姓名：%s->->->性别：%s->->->成绩：%f┃\n", s.num, s.name, s.sex, s.grades);
            }
        }
        fclose(fp);
        break;

    case 5:
        fclose(fp);
        return;

    case 0:
        fclose(fp);
        exit(0);
    }

    printf("\t┃                                                  ┃\n");
    printf("\t┗━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n");

    return;
}
